//
//  HomeWorkoutsRecordsCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import MYModule

import RxCocoa
import RxSwift

open class HomeWorkoutsRecordsCell: HomeBaseCell {
    
    var viewModel: HomeWorkoutsRecordViewModel?
    
    var tapHandle: (() ->Void)?
    
    let targetHourLabel = Label()
    let totalTimesView = TotalValueView()
    let totalCaloriesView = TotalValueView()
    let totalDurationView = TotalValueView()
    
    private let stackView = StackView()
    
   open override func makeUI() {
        super.makeUI()
        self.iconImagView.image = UIImage(named: "ic_home_today_workout_records")
        self.titleLabel.text = "workouts_records_title".language
       
       totalTimesView.titleLabel.text = "workouts_records_total_times".language
       totalCaloriesView.titleLabel.text = "workouts_records_total_calories".language
       totalDurationView.titleLabel.text = "workouts_records_total_duration".language
       
       totalTimesView.valueView.unitLabel.text = "unit_times".language
       totalCaloriesView.valueView.unitLabel.text = "unit_calorie".language
       totalDurationView.valueView.unitLabel.text = "unit_minute".language
       
       targetHourLabel.textColor = UIColor.assist_D0D0D0
       targetHourLabel.font = Fonts.systemFont(ofSize: 10)
       
       totalTimesView.valueView.iconImageView.image = UIImage(named: "ic_home_today_workout_records_times")
       totalCaloriesView.valueView.iconImageView.image = UIImage(named: "ic_home_today_workout_records_calories")
       totalDurationView.valueView.iconImageView.image = UIImage(named: "ic_home_today_workout_records_duration")
       
       measureButton.rx.tap.subscribe(onNext: {[weak self] in
           //TODO: - 暂时关闭是否隐藏测量按钮的判断
           BluetoothResponder.shared.willTrainingment {
               
               self?.tapHandle?()
           }
       }).disposed(by: rx.disposeBag)
       

       stackView.alignment = .center
       stackView.axis = .horizontal
       stackView.distribution = .equalCentering
       
       stackView.addArrangedSubview(totalTimesView)
       stackView.addArrangedSubview(totalCaloriesView)
       stackView.addArrangedSubview(totalDurationView)
       
       bgView.addSubview(stackView)
       bgView.addSubview(targetHourLabel)
       bgView.addSubview(measureButton)
       
       stackView.snp.makeConstraints { make in
           make.left.equalToSuperview()
           make.right.equalToSuperview()
           make.top.equalTo(dateLabel.snp.bottom).offset(20)
       }
       
       targetHourLabel.snp.makeConstraints { make in
           make.right.equalToSuperview().offset(-15)
           make.centerY.equalTo(dateLabel.snp.centerY)
       }
      
      
        measureButton.snp.makeConstraints { make in
            make.top.equalTo(stackView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(15)
            make.height.equalTo(52)
            make.bottom.trailing.equalToSuperview().offset(-15)
        }
        
    }
    
 
    func bindButton(measureState: UIControl.State) {
        
        if measureState == .selected {
            
            measureButton.isEnabled = true
            measureButton.isSelected = true
            
        } else if measureState == .normal {
            
            measureButton.isEnabled = true
            measureButton.isSelected = false
            
        } else if measureState == .disabled {
            
            measureButton.isEnabled = false
            measureButton.isSelected = false
        }
        
    }
    
    public override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeWorkoutsRecordViewModel else {
            return
        }
        
        self.viewModel = viewModel
        let disposeBag = DisposeBag()
        viewModel.UIReplay.subscribe(onNext: {[weak self] model in
            
            self?.bindButton(measureState: model.measureState)
            self?.dateLabel.text = model.date
            self?.targetHourLabel.text = "\(model.targetTimeTitle)\("unit_minute".language)"
            self?.totalTimesView.value = model.totalTimesTitle
            self?.totalCaloriesView.value = model.totalCaloriesTitle
            self?.totalDurationView.value = model.totalDurationTitle
        
        }).disposed(by: disposeBag)
        
        viewModel.hasDevicedReplay.subscribe(onNext: {[weak self] in
               
            self?.updateLayouts(hasDevice: $0)
            
        }).disposed(by: disposeBag)
        
        
    }
    
    func updateLayouts(hasDevice: Bool?) {
        
        let hidden = hasDevice == true ? false : true
        
        measureButton.isHidden = hidden
        
        if !hidden {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom).offset(20)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(52)
                make.bottom.equalToSuperview().offset(-15)
                
            }
            
        } else {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(0)
                make.bottom.equalToSuperview().offset(-15)
                
            }
            
            
        }
        
    }
  
    private lazy var measureButton: BaseButton = {
        let measureButton = BaseButton()
        measureButton.setTitle("workouts_records_start_workout_btn_title".language, for: .normal)
        measureButton.setTitleColor(UIColor.Training.theme, for: .normal)
        measureButton.titleLabel?.font = Fonts.systemFont(ofSize: 14)
        measureButton.backgroundColor = UIColor.assist_F8F8F8
        
        let disableTitleColor = UIColor.assist_A9A9A9
        measureButton.setTitleColor(disableTitleColor, for: .disabled)
        
        measureButton.cornerRadius = 14
        return measureButton
    }()
}


extension HomeWorkoutsRecordsCell {
    
    
    open class TotalValueView: View {
        
       public var value: String? {
            didSet {
                valueView.numberLabel.text = value
            }
        }
        
        public let titleLabel = Label()
        
        public let valueView = ValueView()
        
        open override func makeUI() {
            super.makeUI()
            
            titleLabel.textColor = UIColor.assist_222222
            titleLabel.font = Fonts.systemFont(ofSize: 12)
            
            addSubview(titleLabel)
            addSubview(valueView)
            
            titleLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview()
                make.right.lessThanOrEqualToSuperview()
            }
            
            valueView.snp.makeConstraints { make in
                make.top.equalTo(titleLabel.snp.bottom).offset(3)
                make.centerX.equalTo(titleLabel.snp.centerX)
                make.right.lessThanOrEqualToSuperview()
                make.bottom.equalToSuperview()
            }
            
            
        }
        
    }
    
}

extension HomeWorkoutsRecordsCell.TotalValueView {
    
   open class ValueView: View {
        
        public let iconImageView = ImageView()
        
        public let numberLabel = Label()
        
        public let unitLabel = Label()
        
        open override func makeUI() {
            super.makeUI()
            
            numberLabel.textColor = UIColor.assist_222222
            numberLabel.font = Fonts.boldSystemFont(ofSize: 16)
            
            unitLabel.textColor = UIColor.assist_222222
            unitLabel.font = Fonts.systemFont(ofSize: 10)
            
            addSubview(iconImageView)
            addSubview(numberLabel)
            addSubview(unitLabel)
            
            
            iconImageView.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.centerY.equalToSuperview()
                make.bottom.equalToSuperview()
            }
            
            numberLabel.snp.makeConstraints { make in
                make.left.equalTo(iconImageView.snp.right).offset(5)
                make.centerY.equalToSuperview()
                make.bottom.equalToSuperview()
            }
            
            unitLabel.snp.makeConstraints { make in
                make.left.equalTo(numberLabel.snp.right).offset(2)
                make.bottom.equalTo(numberLabel.snp.bottom).offset(-3)
                make.right.equalToSuperview()
            }
            
            
        }
        
    }
    
}
